package skitur.server;

import java.util.Date;

import skitur.client.SkiturService;
import skitur.shared.FieldVerifier;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import java.util.ArrayList;


/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class SkiturServiceImpl extends RemoteServiceServlet implements
		SkiturService {

	/**
	 * Escape an html string. Escaping data received from the client helps to
	 * prevent cross-site script vulnerabilities.
	 * 
	 * @param html the html string to escape
	 * @return the escaped string
	 */
	private String escapeHtml(String html) {
		if (html == null) {
			return null;
		}
		return html.replaceAll("&", "&amp;").replaceAll("<", "&lt;")
				.replaceAll(">", "&gt;");
	}

	

	@Override
	public String addReport(Date tripDate, String region, String user,
			long conditions, String snowType, long avalancheRisk, String comment)
			throws IllegalArgumentException {
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

		Entity conditionsEntity = new Entity("Conditions");
		conditionsEntity.setProperty("tripDate", tripDate);
		conditionsEntity.setProperty("region", region);
		conditionsEntity.setProperty("user", user);
		conditionsEntity.setProperty("conditions", conditions);
		conditionsEntity.setProperty("snowType", snowType);
		conditionsEntity.setProperty("avalancheRisk", avalancheRisk);
		conditionsEntity.setProperty("comment", comment);
		
		datastore.put(conditionsEntity);
		return "";
	}



	@Override
	public String getUser() {
		UserService userService = UserServiceFactory.getUserService();
		if (userService.isUserLoggedIn()) {
			userService.getCurrentUser().getNickname();
			return userService.getCurrentUser().getNickname();
		} 
		return "Anonymus";
	}
}
